<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品" prop="spuName">
        <el-input
          v-model="queryParams.spuName"
          placeholder="商品"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收藏数量" prop="cfav">
        <el-input
          v-model="queryParams.cfav"
          placeholder="请输入收藏数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打折描述" prop="discountDesc">
        <el-input
          v-model="queryParams.discountDesc"
          placeholder="请输入打折描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="旧价格" prop="oldPrice">
        <el-input
          v-model="queryParams.oldPrice"
          placeholder="请输入旧价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新价格" prop="newPrice">
        <el-input
          v-model="queryParams.newPrice"
          placeholder="请输入新价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="是否删除" prop="isDelete">
        <el-input
          v-model="queryParams.isDelete"
          placeholder="请输入是否删除"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="销售数量" prop="salesVolume">
        <el-input
          v-model="queryParams.salesVolume"
          placeholder="请输入销售数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递类型" prop="expressName">
        <el-input
          v-model="queryParams.expressName"
          placeholder="请输入快递类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sku:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:sku:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sku:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sku:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="skuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="goodSkuId" />-->
<!--      <el-table-column label="商品ID" align="center" prop="goodSpuId" />-->
      <el-table-column label="商品" align="center" prop="spuName" />
      <el-table-column label="收藏数量" align="center" prop="cfav" />
      <el-table-column label="打折描述" align="center" prop="discountDesc" />
      <el-table-column label="旧价格" align="center" prop="oldPrice" />
      <el-table-column label="新价格" align="center" prop="newPrice" />
<!--      <el-table-column label="是否删除" align="center" prop="isDelete" />-->
      <el-table-column label="销售数量" align="center" prop="salesVolume" />
      <el-table-column label="快递类型" align="center" prop="expressName" />
      <el-table-column label="详情页图片" align="center" prop="goodsSkuImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.goodsSkuImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sku:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sku:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改具体商品的参数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="商品" prop="goodSpuId">
          <el-input v-model="form.spuName" placeholder="请输入隶属商品的名字" />
        </el-form-item>-->
        <el-form-item label="商品" prop="spuName">
          <!--          <el-input v-model="form.categoryParentId" placeholder="请输入类别的父类别" />-->
          <el-row class="block-col-2">
            <el-col :span="12">
              <el-dropdown trigger="click">
                <span class="el-dropdown-link">{{spuNameChoice}}<i class="el-icon-arrow-down el-icon--right"></i></span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item,index) in spuNameList"
                                    :key="index"
                                    icon="el-icon-apple"
                                    @click.native="addSpuNameChoice(item)">{{ item.goodTitle }}</el-dropdown-item>

                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item label="收藏数量" prop="cfav">
          <el-input v-model="form.cfav" placeholder="请输入收藏数量" />
        </el-form-item>
        <el-form-item label="打折描述" prop="discountDesc">
          <el-input v-model="form.discountDesc" placeholder="请输入打折描述" />
        </el-form-item>
        <el-form-item label="旧价格" prop="oldPrice">
          <el-input v-model="form.oldPrice" placeholder="请输入旧价格" />
        </el-form-item>
        <el-form-item label="新价格" prop="newPrice">
          <el-input v-model="form.newPrice" placeholder="请输入新价格" />
        </el-form-item>
<!--        <el-form-item label="是否删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除" />
        </el-form-item>-->
        <el-form-item label="销售数量" prop="salesVolume">
          <el-input v-model="form.salesVolume" placeholder="请输入销售数量" />
        </el-form-item>
<!--        <el-form-item label="快递类型" prop="expressTypeId">
          <el-input v-model="form.expressTypeId" placeholder="请输入快递类型" />
        </el-form-item>-->

        <el-form-item label="快递类型" prop="expressName">
          <!--          <el-input v-model="form.categoryParentId" placeholder="请输入类别的父类别" />-->
          <el-row class="block-col-2">
            <el-col :span="12">
              <el-dropdown trigger="click">
                <span class="el-dropdown-link">{{expressNameChoice}}<i class="el-icon-arrow-down el-icon--right"></i></span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item,index) in expressNameList"
                                    :key="index"
                                    icon="el-icon-apple"
                                    @click.native="addExpressNameChoice(item)">{{ item.expressTypeName }}</el-dropdown-item>

                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-form-item>


        <el-form-item label="详情图片">
          <image-upload v-model="form.goodsSkuImage"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSku, getSku, delSku, addSku, updateSku, listSkuWithGoodNameAndExpressName } from '@/api/system/sku'
import { findParentIdByName, listCategoryAndName } from '@/api/system/category'
import { listSpu } from '@/api/system/spu'
import { listExpress } from '@/api/system/express'

export default {
  name: "Sku",
  data() {
    return {
      // 快递类型表格数据
      expressNameList: [],
      //新增类别选中
      expressNameChoice: '请选择快递类型',
      // Spu商品表格数据
      spuNameList: [],
      //新增商品名字选中
      spuNameChoice: '请选择商品',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 具体商品的参数表格数据
      skuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodSpuId: null,
        cfav: null,
        discountDesc: null,
        oldPrice: null,
        newPrice: null,
        isDelete: null,
        salesVolume: null,
        expressTypeId: null,
        goodsSkuImage: null,
        spuName: null,
        expressName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getSpuList();
    this.getExpressList();
  },
  methods: {
    /** 新增快递类型名字 下拉框选中*/
    addExpressNameChoice(item) {
      console.log(item)
      this.expressNameChoice = item.expressTypeName
      this.form.expressTypeId = item.expressTypeId
    },
    /** 查询快递类型名字 的列表*/
    getExpressList() {
      listExpress().then(response => {
        console.log(response.rows)
        this.expressNameList = response.rows

      })
    },
    /** 新增商品名字 下拉框选中*/
    addSpuNameChoice(item) {
      this.spuNameChoice = item.goodTitle
      this.form.goodSpuId = item.goodSpuId
    },
    /** 查询spu名字的列表*/
    getSpuList() {
      listSpu().then(response => {
        this.spuNameList = response.rows;
      });
    },
    /** 查询具体商品的参数列表 */
    getList() {
      this.loading = true;
      listSkuWithGoodNameAndExpressName(this.queryParams).then(response => {
        //console.log(response.rows)
        this.skuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodSkuId: null,
        goodSpuId: null,
        cfav: null,
        discountDesc: null,
        oldPrice: null,
        newPrice: null,
        createTime: null,
        isDelete: null,
        salesVolume: null,
        expressTypeId: null,
        goodsSkuImage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodSkuId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加具体商品的参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodSkuId = row.goodSkuId || this.ids
      getSku(goodSkuId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改具体商品的参数";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodSkuId != null) {
            updateSku(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSku(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodSkuIds = row.goodSkuId || this.ids;
      this.$modal.confirm('是否确认删除具体商品的参数编号为"' + goodSkuIds + '"的数据项？').then(function() {
        return delSku(goodSkuIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sku/export', {
        ...this.queryParams
      }, `sku_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
